package de.hfu.android;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.LiveFolders;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import de.hfu.helper.DataAccess;
import de.hfu.helper.DataCarrier;
import de.hfu.helper.DataParser;
import de.hfu.model.DayPlan;
import de.hfu.model.Dish;

public class WeekViewActivity extends Activity implements OnClickListener {
	WeekViewListManager listViewManager;
	UpdateFromTheInternetTask updator;
	boolean firstTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		PreferenceManager.setDefaultValues(this, R.xml.preferences, true);

		// SharedPreferences prefs =
		// PreferenceManager.getDefaultSharedPreferences(this);
		setContentView(R.layout.weekview);

		listViewManager = new WeekViewListManager(this);
		updator = new UpdateFromTheInternetTask(this, listViewManager);
		updator.execute();

		Button btnRefresh = (Button) findViewById(R.id.btn_refresh);
		btnRefresh.setOnClickListener(this);

		firstTime = true;
		
		applyChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// menu.add(0, MENU_SETTINGS, 0, "Settings");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// if (item.getItemId() == MENU_SETTINGS) {
		// Intent i = new Intent(this, SettingActivity.class);
		// startActivity(i);
		// return true;
		// }
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent i = new Intent(this, SettingActivity.class);
			startActivity(i);
			return true;
		case R.id.about:
			Intent intent = new Intent(this, AboutActivity.class);
			startActivity(intent);
			return true;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStart() {
		super.onStart();

		TextView lblCampus = (TextView) findViewById(R.id.lblCampus);
		lblCampus.setText(DataAccess.getInstance(this).getCampusName());

		if (!firstTime)
			listViewManager.reloadData();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		boolean changed = prefs.getBoolean("changedInternet", false);
		
		if (changed) {
			new UpdateFromTheInternetTask(this, listViewManager).execute();		
			applyChanged();
		} else {
			changed = prefs.getBoolean("changedLocal", false);
			if (changed) {
				new RetrieveDataTask(this, listViewManager.adapter,
						listViewManager.flagLoadAll).execute();
				applyChanged();
			}
		}
	}

	@Override
	public void onClick(View v) {
		updator = new UpdateFromTheInternetTask(this, listViewManager);
		updator.execute();
		applyChanged();
	}
	
	private void applyChanged() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean("changedInternet", false);
		editor.putBoolean("changedLocal", false);
		editor.commit();
	}

}
